package com.itheima.web.controller.system.syslog;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.SysLog;
import com.itheima.service.system.SysLogService;
import com.itheima.service.system.impl.SysLogServiceImpl;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@WebServlet("/system/sysLog")
public class SysLogServlet extends BaseServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");

        //根据请求参数的不同执行不同的功能
        if ("list".equals(operation)) {
            list(request, response);
        } else if ("deleteMany".equals(operation)) {
            deleteMany(request, response);
        }
    }

    private void deleteMany(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String moduleIds = request.getParameter("moduleIds");
//        System.out.println(moduleIds);
        String[] str = moduleIds.split(",");

        for (String s : str) {
//            System.out.println(s);
            sysLogService.delete(s);
        }

        response.sendRedirect(request.getContextPath() + "/system/sysLog?operation=list");

        //记日志
        SysLogServlet.logMethodForServlet(request);

    }



    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*List<SysLog> sysLogs = sysLogService.findAll();
        request.setAttribute("sysLogs", sysLogs);*/

        //进入列表页,获取数据
        int currentPage = 1;
        int pageSize = 5;

        if (StringUtils.isNotBlank(request.getParameter("currentPage"))) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }

        PageInfo<SysLog> pageInfo = sysLogService.findAll(currentPage, pageSize);

        //将数据保存到指定的位置
        request.setAttribute("page", pageInfo);


        //记日志
        SysLogServlet.logMethodForServlet(request);

        request.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(request, response);

    }


    //记日志的静态方法
    public static void logMethodForServlet(HttpServletRequest request){
        SysLogService mySysLogService = new SysLogServiceImpl();

        SysLog log = (SysLog) request.getAttribute("log");
        log.setExecutionTime((new Date().getTime() - log.getVisitTime().getTime()));

        //System.out.println(log);
        String id = UUID.randomUUID().toString();
        log.setId(id);

        //ID            自动生成
        //访问时间      数据库自动
        //访问用户      (admin)
        //访问ip          (已获得ipv6地址)
        //资源URL       已解决
        //执行时间      已解决
        //访问方法      已解决


        //保存日志对象
        //mySysLogService.save(log);
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
